package com.heima.wemadia.controller;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dto.ChannelDto;
import com.heima.model.wemedia.dto.WmChannelDao;
import com.heima.model.wemedia.entity.WmChannel;
import com.heima.wemadia.service.WmChannelService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 自媒体频道表(WmChannel)表控制层
 *
 * @author makejava
 * @since 2022-04-12 15:37:55
 */
@RestController
@RequestMapping("/api/v1/channel")
public class WmChannelController {
    /**
     * 服务对象
     */
    @Resource
    private WmChannelService wmChannelService;

    @GetMapping("/channels")
    public ResponseResult list() {

        return wmChannelService.list();
    }

    @PostMapping("/list")
    public ResponseResult ChannelList(@RequestBody ChannelDto dto){

        return wmChannelService.ChannelList(dto);
    }

    @GetMapping("/del/{id}")
    public ResponseResult delById(@PathVariable("id")Long id){
        if(id ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        wmChannelService.deleteById(id);
        return ResponseResult.okResult("删除成功");
    }

    @PostMapping("/save")
    public ResponseResult save(@RequestBody WmChannelDao dao){
        WmChannel wmChannel = getWmChannel(dao);
        return wmChannelService.save(wmChannel);
    }

    @PostMapping("/update")
    public ResponseResult update(@RequestBody WmChannelDao dao){
        WmChannel wmChannel = getWmChannel(dao);
        wmChannelService.update(wmChannel);
        return ResponseResult.okResult("更新成功");
    }

    private WmChannel getWmChannel(WmChannelDao dao) {
        WmChannel wmChannel = new WmChannel();
        BeanUtils.copyProperties(dao, wmChannel);
        if (dao.getStatus()) {
            wmChannel.setStatus(1);
        } else {
            wmChannel.setStatus(0);
        }
        if (dao.getIsDefault()) {
            wmChannel.setIsDefault(1);
        } else {
            wmChannel.setIsDefault(0);
        }
        return wmChannel;
    }

}

